package org.byqh.dqeai.controller;

import io.swagger.v3.oas.annotations.tags.Tag;
import org.apache.commons.lang3.StringUtils;
import org.byqh.dqeai.common.Json;
import org.byqh.dqeai.dto.common.IPage;
import org.byqh.dqeai.dto.common.ListSaveDTO;
import org.byqh.dqeai.entity.SysAppModuleFileCategory;
import org.byqh.dqeai.entity.proxy.SysAppModuleFileCategoryProxy;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/sysAppModuleFileCategory")
@Tag(name = "系统[SysAppModuleFileCategory] - 应用模块文件分类管理")
public class SysAppModuleFileCategoryController extends BaseController<SysAppModuleFileCategory, SysAppModuleFileCategoryProxy> {
    /**
     * 删除一条记录
     */
    @DeleteMapping(value = "/{id}")
    public Json<Boolean> delete(@PathVariable String id) {
        try {
            var rows = getEntityDeletable()
                    .where(s -> s.appModuleId().eq(id))
                    .executeRows();

            return rows > 0 ? Json.success() : Json.error();
        } catch (Exception e) {
            return Json.exception(e);
        }
    }

    /**
     * 分页查询
     *
     * @param page
     * @param size
     * @param sort
     * @param dto
     * @param userId
     * @return
     */
    @PostMapping(value = "/list")
    public Json<IPage<SysAppModuleFileCategory>> list(@RequestParam(defaultValue = "1") Integer page,
                                                      @RequestParam(defaultValue = "10") Integer size,
                                                      @RequestParam(defaultValue = "", required = false) String sort,
                                                      @RequestBody(required = false) SysAppModuleFileCategory dto) {

        return this.listPageImpl(SysAppModuleFileCategory.class, page, size, (query) -> {
            if (dto != null) {
                if (!StringUtils.isBlank(dto.getAppModuleId())) {
                    query = query.where(r -> r.appModuleId().eq(dto.getAppModuleId()));
                }
            }

            return query;
        });
    }

    /**
     * 批量更新
     */
    @PostMapping(value = "/listSave")
    public Json<Boolean> listSave(@RequestBody() ListSaveDTO<SysAppModuleFileCategory> dto) {
        if (!StringUtils.isBlank(dto.getDeleteId())) {
            easyEntityQuery.deletable(SysAppModuleFileCategory.class)
                    .disableLogicDelete()
                    .allowDeleteStatement(true)
                    .where(s -> s.appModuleId().eq(dto.getDeleteId()))
                    .executeRows();
        }

        if (dto.getList() == null || dto.getList().isEmpty()) {
            return Json.success(true);
        }

        return this.saveBatch(dto.getList());
    }
}